package com.todo.event.activity.application.handler.activityreward;

import com.todo.event.activity.application.entity.context.RewardSendContext;
import com.todo.event.activity.application.remote.RewardRemote;
import com.todo.event.activity.application.service.EventActivityRecordService;
import com.todo.event.activity.common.enums.EventActivityExecuteRewardEnum;
import com.todo.event.activity.common.enums.ExecuteRecordStatusEnum;
import com.todo.event.activity.common.enums.RecordTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2025/1/13 20:13
 * @description
 */
@Slf4j
@Component
public class GrowthHandle extends AbstractRewardHandle{

    @Resource
    private EventActivityRecordService eventActivityRecordService;
    @Resource
    private RewardRemote rewardRemote;

    @Override
    public boolean preHandle(RewardSendContext context) {
        //如果为对照组，则更新记录状态为成功
        if (context.getIsCompare()) {
            eventActivityRecordService.updateRecordBaseInfo(RecordTypeEnum.REWARD, context.getRecordId(), "compared", null, ExecuteRecordStatusEnum.SUCCESS);
            return true;
        }
        //后续可以验证对应模板和数据是否有余量等
        return false;
    }

    @Override
    public Boolean doHandle(RewardSendContext context) {
        String propertyCode = rewardRemote.sendReward();
        boolean result = eventActivityRecordService.updateRecordBaseInfo(RecordTypeEnum.REWARD, context.getRecordId(), propertyCode,null, ExecuteRecordStatusEnum.SUCCESS);
        return result;
    }

    @Override
    public EventActivityExecuteRewardEnum getReward() {
        return EventActivityExecuteRewardEnum.GROWTH;
    }
}
